package com.uzai.console.service.mini.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.mini.miniaddress.MiniAddressDto;
import com.uzai.console.dto.mini.miniaddress.MiniAddressIdDto;
import com.uzai.console.dto.mini.miniaddress.MiniAddressQuery;
import com.uzai.console.dto.mini.miniaddress.MiniAreaByParentDto;
import com.uzai.console.entity.MiniAddress;
import com.uzai.console.entity.MiniArea;
import com.uzai.console.mapper.MiniAddressMapper;
import com.uzai.console.mapper.MiniAreaMapper;
import com.uzai.console.service.mini.MiniAddressService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.mini.miniaddress.MiniAddressVo;
import com.uzai.console.vo.mini.miniaddress.MiniAreaVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class MiniAddressServiceImpl implements MiniAddressService {

    @Autowired
    private MiniAddressMapper miniAddressMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MiniAreaMapper miniAreaMapper;

    /**
     * 分页查询小程序团购商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return MiniAddressVo
     */
    public Page<MiniAddressVo> findMiniAddressByPage(MiniAddressQuery miniAddressQuery){
        //新建返回到页面的LIST对象
        List<MiniAddressVo> miniAddressVoList = new ArrayList<>();
        //返回到页面分页对象
        Page<MiniAddressVo> pageVo = new Page<>();

        //执行分页查询对象
        Page<MiniAddress> page = new Page<>();
        miniAddressQuery.convert(page);
        List<MiniAddress> miniAddressList = miniAddressMapper.findByList(page, miniAddressQuery);

        if(miniAddressList != null && miniAddressList.size() > 0){
            for (MiniAddress miniAddress : miniAddressList){
                MiniAddressVo miniAddressVo = new MiniAddressVo();
                BeanUtils.copyProperties(miniAddress, miniAddressVo);
                miniAddressVoList.add(miniAddressVo);
            }
        }

        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(miniAddressVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询小程序团购商品详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return MiniAddressVo
     */
    public MiniAddressVo findMiniAddressById(MiniAddressIdDto miniAddressIdDto){

        //返回的对象
        MiniAddressVo miniAddressVo = new MiniAddressVo();

        if(miniAddressIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        MiniAddress miniAddress = miniAddressMapper.selectById(miniAddressIdDto.getId(), miniAddressIdDto.getMerId());
        if(miniAddress == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        BeanUtils.copyProperties(miniAddress, miniAddressVo);

        return miniAddressVo;
    }

    /**
     * 修改小程序团购商品详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param miniAddressDto
     * @return null
     */
    @Transactional
    public void addMiniAddress(MiniAddressDto miniAddressDto){

        //如果新添加的为默认，则先将其他的全部修改成非默认
        if(Tools.getInteger(miniAddressDto.getIsDefault()).intValue() == 1){
            //修改运营商所有的地址为非默认(type=1)
            miniAddressMapper.updateIsNotDefault(1, miniAddressDto.getMerId());
        }

        int now = DateUtil.getNowTime();
        MiniAddress miniAddress = new MiniAddress();
        miniAddress.setId(IdWorker.getId());
        miniAddress.setMerId(miniAddressDto.getMerId());
        miniAddress.setReceiverName(miniAddressDto.getReceiverName());
        miniAddress.setReceiverMobile(miniAddressDto.getReceiverMobile());
        miniAddress.setProvince(miniAddressDto.getProvince());
        miniAddress.setProvinceCode(miniAddressDto.getProvinceCode());
        miniAddress.setCity(miniAddressDto.getCity());
        miniAddress.setCityCode(miniAddressDto.getCityCode());
        miniAddress.setArea(miniAddressDto.getArea());
        miniAddress.setAreaCode(miniAddressDto.getAreaCode());
        miniAddress.setStreet(miniAddressDto.getStreet());
        miniAddress.setDetail(miniAddressDto.getDetail());
        miniAddress.setType(1);       //运营商地址
        miniAddress.setIsDefault(miniAddressDto.getIsDefault());
        miniAddress.setRemark(miniAddressDto.getRemark());
        miniAddress.setVersion(0);
        miniAddress.setCreateTime(now);
        miniAddress.setUpdateTime(now);
        miniAddressMapper.insertSelective(miniAddress);
    }

    /**
     * 修改小程序团购商品详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param miniAddressDto
     * @return null
     */
    public void updateMiniAddress(MiniAddressDto miniAddressDto){

        if(miniAddressDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        MiniAddress miniAddress = miniAddressMapper.selectById(miniAddressDto.getId(), miniAddressDto.getMerId());

        if(miniAddress == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        int now = DateUtil.getNowTime();

        //如果新添加的为默认，则先将其他的全部修改成非默认
        if(Tools.getInteger(miniAddressDto.getIsDefault()).intValue() == 1){
            //修改运营商所有的地址为非默认(type=1)
            miniAddressMapper.updateIsNotDefault(1, miniAddressDto.getMerId());
        }

        miniAddress.setReceiverName(miniAddressDto.getReceiverName());
        miniAddress.setReceiverMobile(miniAddressDto.getReceiverMobile());
        miniAddress.setProvince(miniAddressDto.getProvince());
        miniAddress.setProvinceCode(miniAddressDto.getProvinceCode());
        miniAddress.setCity(miniAddressDto.getCity());
        miniAddress.setCityCode(miniAddressDto.getCityCode());
        miniAddress.setArea(miniAddressDto.getArea());
        miniAddress.setAreaCode(miniAddressDto.getAreaCode());
        miniAddress.setStreet(miniAddressDto.getStreet());
        miniAddress.setDetail(miniAddressDto.getDetail());
        miniAddress.setIsDefault(miniAddressDto.getIsDefault());
        miniAddress.setRemark(miniAddressDto.getRemark());
        miniAddress.setVersion(0);
        miniAddress.setCreateTime(now);
        miniAddress.setUpdateTime(now);
        miniAddressMapper.updateById(miniAddress);
    }

    /**
     * 删除小程序团购商品信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void deleteMiniAddress(MiniAddressIdDto miniAddressIdDto){
        if(miniAddressIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        MiniAddress miniAddress = miniAddressMapper.selectById(miniAddressIdDto.getId(), miniAddressIdDto.getMerId());

        if(miniAddress == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        miniAddressMapper.deleteById(miniAddressIdDto.getId(), miniAddressIdDto.getMerId());

    }

    /**
     * 查询地址区域列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public List<MiniAreaVo> fingAreaListByParent(MiniAreaByParentDto miniAreaByParentDto){
        List<MiniAreaVo> selectKeyVoList = new ArrayList<>();
        List<MiniArea> miniAreaList = miniAreaMapper.findListByParent(miniAreaByParentDto.getParent());
        if(miniAreaList != null && miniAreaList.size() > 0){
            for (MiniArea miniArea : miniAreaList) {
                MiniAreaVo miniAreaVo = new MiniAreaVo();
                BeanUtils.copyProperties(miniArea, miniAreaVo);
                selectKeyVoList.add(miniAreaVo);
            }
        }
        return selectKeyVoList;
    }

}
